<?php

namespace app\admin\controller;

use app\admin\model\Column as mColumn;
use think\Request;
use think\Db;

class Column extends Base
{

    protected $beforeActionList = [
        'beforeEdit' => ['only' => 'edit'],
        'beforeClassify' => ['only' => 'edit,add'],
    ];

    //加载所要编辑数据
    protected function beforeEdit()
    {
        //修改前置操作
        $request = Request::instance();
        if ($request->isGet() && $request->has('id')) {
            $data = Db::name('column')->find($request->param('id'));
            $data['status'] = $data['status'] == 1 ? TRUE : FALSE;
            $this->assign(['data' => json_encode($data)]);
        } else {
            $this->error('请求参数错误', 'Index/welcome');
        }
    }

    //加载分类
    protected function beforeClassify()
    {
        //修改前置操作，判断如果
        $request = Request::instance();

        $data = infinite_classify_1w('column');
        if (isset($data['code'])) {
            return $data;
        }
//        halt($data);
        $this->assign(['columnList' => $data]);

    }

    /*页面加载
     * */
    public function index()
    {
        return $this->fetch();
    }

    /*列表数据调用
 * */
    public function lists(Request $request)
    {
        if ($request->isPost()) {
            //判断是否有条件查询
            $where = $request->has('where') ? $request->param()['where'] : [];
            //获取当前页码
            $page = $request->param('page');
            //获取每页显示条数
            $limit = $request->param('limit');
            //过滤空值项
            $where = array_filter($where);

            return mColumn::mAll($page, $limit, $where);
        } else {
            return return_json(1, '请求错误！');
        }
    }

    /*增加(页面加载)
  * */
    public function add()
    {
        return $this->fetch();
    }

    /*
     * 修改(页面加载)
     * */
    public function edit()
    {
        return $this->fetch();
    }

    /*公共：数据增加或者修改
     *
     * */
    public function save(Request $request)
    {
        //判断是否是post请求过来数据
        if ($request->isPost()) {

            //定义默认为增加操作
            $method = ['create', '增加'];
            $data = $request->param()['data'];

            //判断是否是更新数据
            if ($request->has('data.id')) {
                $method = ['update', '更新'];
            }
            //验证输入数据是否合法
            $check = $this->validate($data, $request->controller() . '.'  . $method[0]);
            if (TRUE !== $check) {
                return return_json(1, $check);
            }

            //调用模型增加或者更新静态方法
            return mColumn::mSave($data, $method);
        } else {
            return return_json(1, '请求错误！');
        }
    }

    /*
  * 删除
  * */
    public function del(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->param()['data'];
            return mColumn::mDel($data);
        } else {
            $this->error('请求参数错误', 'index/welcome');
        }
    }

    /*单元格修改
    * */
    public function cellEdit(Request $request)
    {
        if ($request->isPost()) {

            $data = $request->param()['data'];
//            halt($data);
            //验证输入数据是否合法
            $check = $this->validate($data, 'column.cellEdit');
            if (TRUE !== $check) {
                return return_json(1, $check);
            }

            return mColumn::mCellEdit($data);
        } else {
            $this->error('请求参数错误', 'index/welcome');
        }
    }
}
